package cn.kshost.zqyg.service.pay.service.impl;

import cn.kshost.zqyg.client.order.OrderFeign;
import cn.kshost.zqyg.client.product.ProductFeign;
import cn.kshost.zqyg.model.dto.product.SkuSaleDto;
import cn.kshost.zqyg.model.entity.order.OrderInfo;
import cn.kshost.zqyg.model.entity.order.OrderItem;
import cn.kshost.zqyg.model.entity.pay.PaymentInfo;
import cn.kshost.zqyg.service.pay.mapper.PaymentInfoMapper;
import cn.kshost.zqyg.service.pay.service.PaymentInfoService;
import com.alibaba.fastjson.JSON;
import com.alipay.api.domain.SalesData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ProductFeign productFeign;

    @Override
    public PaymentInfo addPaymentInfo(String orderNo) {

        //根据订单id查询订单信息 查看是否存在订单
        PaymentInfo PaymentInfo =  paymentInfoMapper.selectPaymentInfoByOrderNo(orderNo);
        if(PaymentInfo == null){
            OrderInfo orderInfo = orderFeign.getOrderInfoByOrderNo(Long.valueOf(orderNo)).getData();
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setOrderNo(orderNo);
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPayType(orderInfo.getPayType());
            String content = "";
            for (OrderItem orderItem : orderInfo.getOrderItemList()) {
                content += orderItem.getSkuName();
            }
            paymentInfo.setContent(content);
            paymentInfo.setAmount(orderInfo.getTotalAmount());
            paymentInfo.setPaymentStatus(0);
            paymentInfoMapper.insertPaymentInfo(paymentInfo);
            return paymentInfo;
        }
        return  PaymentInfo;
    }

    @Override
    public void handlePaymentInfo(Map<String, String> params, Integer payType) {
        //查询payment info信息
        PaymentInfo paymentInfo = paymentInfoMapper.selectPaymentInfoByOrderNo(params.get("out_trade_no"));
        if (paymentInfo.getPaymentStatus() == 1) {
            return;
        }
        //更新paymentinfo支付信息
        paymentInfo.setPayType(payType);
        paymentInfo.setPaymentStatus(1);
        paymentInfo.setOutTradeNo(params.get("trade_no"));
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(params));
        paymentInfoMapper.updateById(paymentInfo);
        //更新订单支付状态和订单日志状态
        orderFeign.updateOrderStatus(paymentInfo.getOrderNo(),payType);
        //更新商品销量和库存
        OrderInfo orderInfo = orderFeign.getOrderInfoByOrderNo(Long.valueOf(paymentInfo.getOrderNo())).getData();
        List<SkuSaleDto> skuSaleDtos = orderInfo.getOrderItemList().stream().map(orderItem -> {
            SkuSaleDto saleDto = new SkuSaleDto();
            Long skuId = orderItem.getSkuId();
            Integer skuNum = orderItem.getSkuNum();
            saleDto.setSkuId(skuId);
            saleDto.setNum(skuNum);
            return saleDto;
        }).collect(Collectors.toList());
        productFeign.updateSkuSaleNumAndStock(skuSaleDtos);


    }
}
